package config

import (
	"fmt"
	"path/filepath"

	"torler.com/qingyue/pkg/env"
	"torler.com/qingyue/pkg/logger"
	"torler.com/qingyue/pkg/storage"
)

type Config struct {
	Log   *logger.LogConfig    `mapstructure:"log"`
	DB    *storage.DBConfig    `mapstructure:"db"`
	Redis *storage.RedisConfig `mapstructure:"redis"`
	Rule  *Rule                `mapstructure:"rule"`
}

var conf Config

func LoadConfig() error {
	name := fmt.Sprintf("%s.yml", env.GetMode())
	var cfg Config
	configPath := filepath.Join(env.GetConfigDir(), name)
	err := env.LoadFromFile(&cfg, configPath)
	if err != nil {
		return err
	}
	conf = cfg
	return nil
}

func GetConfig() *Config {
	return &conf
}
